<?php
class Photo_model extends MY_Model {
	private $table = 'photo';

	function getsearchContent($limit = 10,$page = 0){
		$this->db->select('a.name AS album_name,p.*');
		$this->db->from(PREFIX.'photo AS p');
		$this->db->join(PREFIX.'album AS a','a.id = p.album_id','LEFT');
		$this->db->limit($limit,$page);
		$this->db->order_by('p.album_id','ASC');
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('`name` LIKE "%'.$this->input->post('content').'%"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get();

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getTotalsearchContent(){
		$this->db->select('*');
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->or_where('`name` LIKE "%'.$this->input->post('content').'%"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.'photo');
		
		if($query->result()){
			return count($query->result());
		}else{
			return false;
		}
	}
	
	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function saveManagement($file_name){	 //,&$return_id
		if($this->input->post('status')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}
		
		if($this->input->post('is_home')=='on'){
			$is_home = 1;
		}else{
			$is_home = 0;
		}
		
		if($this->input->post('hiddenId')== 0){
			$data = array(
				'link'		=> $file_name,
				'video_link'=> $this->input->post('video_link'),
				'album_id'	=> $this->input->post('album_id'),
				'is_home'	=> $is_home,
				'status'	=> $status,
				'created'	=> date('Y-m-d H:i:s',time()),
				'modified'	=> date('Y-m-d H:i:s',time())
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				//$return_id = $this->db->insert_id();
				return true;
			}
		}else{
			$result = $this->getDetailManagement($this->input->post('hiddenId'));
			if($file_name ==''){
				$file_name = $result[0]->link;
			}else{
				@unlink(DIR_PHOTO.$result[0]->link);
				@unlink(DIR_PHOTO_THUMB.$result[0]->link);
			}
			
			$data = array(
				'link'			=> $file_name,
				'video_link'	=> $this->input->post('video_link'),
				'album_id'		=> $this->input->post('album_id'),
				'status'		=> $status,
				'modified'		=> date('Y-m-d H:i:s',time())
			);
			$this->db->where('id',$this->input->post('hiddenId'));
			if($this->db->update(PREFIX.$this->table,$data)){
				//$return_id = $this->input->post('hiddenId');
				return true;
			}
		}
		return false;
	}
	
	public function getParentCat($id = 2)
	{
		$this->db->select('*');
		$this->db->where('parent_id',$id);
		$this->db->where('status',1);
		$query = $this->db->get(PREFIX.'category');
		$result = $query->result();
		if($result)
		{
			return $result;
		}else{
			return $result;
		}
	}
	
	function getCatDetail($id = 0)
	{
		$this->db->select('*');
		$this->db->where('parent_id',$id);
		$this->db->where('status',1);
		$this->db->order_by('name','DESC');
		$query = $this->db->get(PREFIX.'album');
		$result = $query->result();
		if($result)
		{
			return $result;
		}else{
			return $result;
		}
	}
}